package 尚硅谷;

import solution.Solution_5sortandSearch;

import java.security.ProtectionDomain;
import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author zhangmin
 * @create 2022-03-23 15:16
 * 集合类不安全问题
 *  ArrayList
 *  Set
 *  Map
 */
public class ContainerNotSafeDemo {
    public static void main(String[] args) {
        Map<String,String > map=new HashMap<>();
        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                map.put(Thread.currentThread().getName(),UUID.randomUUID().toString().substring(0,8));
                System.out.println(map);
            },String.valueOf(i)).start();
        }
    }
    public static void listNotSafe(){
        //List<String> list = new ArrayList<>();
        List<String> list= Collections.synchronizedList(new ArrayList<>());
        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                list.add(UUID.randomUUID().toString().substring(0,8));
                System.out.println(list);
            },String.valueOf(i)).start();
        }
    }
    public static void setNotSafe(){
        //Set<String> set=Collections.synchronizedSet(new HashSet<>());
        Set<String> set=new CopyOnWriteArraySet<>();
        for (int i = 0; i < 30; i++) {
            new Thread(()->{
                set.add(UUID.randomUUID().toString().substring(0,8));
                System.out.println(set);
            },String.valueOf(i)).start();
        }
    }
}
